package com.SouthernWall_404.AgeDust.Client.Gui.Screen;

import com.SouthernWall_404.AgeDust.Common.API.Info.AttributesInfo;
import com.SouthernWall_404.AgeDust.Common.API.Info.CharacterInfo;
import com.SouthernWall_404.AgeDust.Common.AgeDust;
import com.SouthernWall_404.AgeDust.Common.Network.C2S.CharacterScreenClientPacket;
import com.SouthernWall_404.AgeDust.Common.Network.ModChannels;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;

import java.util.ArrayList;
import java.util.List;

public class AttributeScreen extends Screen {

    private int time_point=0;

    private static final String GUI="gui."+ AgeDust.MODID+".attribute_screen.";
    private int GAP_X =10;//间隔
    private int GAP_Y=3;//间隔
    private int h=20;//按键高度
    private int l1 =60;//标签键宽度
    private int l2=30;//按钮键宽度

    private int default_x=0;
    private int default_y=0;
    private int current_y=0;
    private int current_x=0;

    public int[] levelArrayToChange = {0,0,0,0,0,0,0,0};

    public AttributesInfo attributesInfo=new AttributesInfo();
    public CharacterInfo characterInfo=new CharacterInfo();

    private Button timePointButton= Button.builder(Component.translatable(GUI+"button.time_point",time_point),b->{}).build();

    private List<List<Button>> attributeButtons=new ArrayList<>();


    public AttributeScreen(Component p_96550_,CharacterInfo characterInfo,AttributesInfo attributesInfo) {


        super(p_96550_);

        this.characterInfo=characterInfo;
        this.attributesInfo=attributesInfo;

        this.time_point=100;

        levelArrayToChange =attributesInfo.levelArray;

    }

    @Override
    public void render(GuiGraphics p_281549_, int p_281550_, int p_282878_, float p_282465_) {
        this.renderBackground(p_281549_);
        super.render(p_281549_, p_281550_, p_282878_, p_282465_);
    }

    @Override
    public void renderBackground(GuiGraphics guiGraphics) {
        super.renderBackground(guiGraphics);

    }

    @Override
    protected void init() {
        super.init();
        this.attributeButtons.clear();
        this.clearWidgets();

        this.current_x=(this.width-GAP_X*3-this.l1*2-this.l2*6)/2;
        this.current_y=(this.height- GAP_Y *7-this.h*8)/2;

        this.default_x=this.current_x;
        this.default_y=this.current_y;

        addWidget();
    }
    private int counter = 0;
    private long lastUpdateTime = 0;
    @Override
    public void tick() {
        super.tick();

//        if (System.currentTimeMillis() - lastUpdateTime > 1000) {
//            lastUpdateTime = System.currentTimeMillis();
//            counter++;
//            updateButtonText();
//        }
    }

    private Button createTagButton(String string)
    {
        Component component=Component.translatable(GUI+"button."+string);
        return Button.builder(component,b->{}).build();
    }

    private List<Button> createOperateButton(int index)
    {
        List<Button> operateButton=new ArrayList<>();
        Component component=Component.translatable(GUI+"button.level", levelArrayToChange[index]);

        Button levelButton= Button.builder(component,b->{}).build();


        Button minusOneButton=Button.builder(Component.literal("-1"),b->addLevel(index,-1,levelButton)).build();
        minusOneButton.setHeight(this.h);
        minusOneButton.setWidth(this.l2);
        operateButton.add(minusOneButton);

        Button minusFiveButton=Button.builder(Component.literal("-5"),b->addLevel(index,-5,levelButton)).build();
        minusFiveButton.setHeight(this.h);
        minusFiveButton.setWidth(this.l2);
        operateButton.add(minusFiveButton);

        Button minusTenButton=Button.builder(Component.literal("-10"),b->addLevel(index,-10,levelButton)).build();
        minusTenButton.setHeight(this.h);
        minusTenButton.setWidth(this.l2);
        operateButton.add(minusTenButton);


        levelButton.setHeight(this.h);
        levelButton.setWidth(this.l1);
        operateButton.add(levelButton);

        Button addOneButton=Button.builder(Component.literal("+1"),b->addLevel(index,1,levelButton)).build();
        addOneButton.setHeight(this.h);
        addOneButton.setWidth(this.l2);
        operateButton.add(addOneButton);

        Button addFiveButton=Button.builder(Component.literal("+5"),b->addLevel(index,5,levelButton)).build();
        addFiveButton.setHeight(this.h);
        addFiveButton.setWidth(this.l2);
        operateButton.add(addFiveButton);

        Button addTenButton=Button.builder(Component.literal("+10"),b->addLevel(index,10,levelButton)).build();
        addTenButton.setHeight(this.h);
        addTenButton.setWidth(this.l2);
        operateButton.add(addTenButton);

        this.attributeButtons.add(index,operateButton);

        this.avalibleCheck();

        return operateButton;
    }
    /*
    操作函数，用于对某一属性进行操作
     */
    private void addLevel(int index, int amount,Button levelButton)
    {

        List<Button> Buttons=attributeButtons.get(index);

        levelArrayToChange[index]+=amount;
        time_point-=amount;

        Component component=Component.translatable(GUI+"button.level", levelArrayToChange[index]);
        Component component1=Component.translatable(GUI+"button.time_point",time_point);
        levelButton.setMessage(component);
        timePointButton.setMessage(component1);

        this.avalibleCheck();

        //addWidget();

    }

    private void avalibleCheck()
    {
        for(int i=0;i<this.attributeButtons.size();i++)
        {
            int index=i;
            List<Button> buttons=this.attributeButtons.get(index);
            if(this.time_point<=0)
            {
                buttons.get(4).active=false;
            }
            else
            {
                buttons.get(4).active=true;
            }
            if(this.time_point<5)
            {
                buttons.get(5).active=false;
            }
            else
            {
                buttons.get(5).active=true;
            }
            if(this.time_point<10)
            {
                buttons.get(6).active=false;
            }
            else
            {
                buttons.get(6).active=true;
            }
            if(levelArrayToChange[index]<10)
            {
                buttons.get(2).active=false;
            }
            else
            {
                buttons.get(2).active=true;
            }
            if(levelArrayToChange[index]<5)
            {
                buttons.get(1).active=false;
            }
            else
            {
                buttons.get(1).active=true;
            }
            if(levelArrayToChange[index]<1)
            {
                buttons.get(0).active=false;
            }

            else
            {
                buttons.get(0).active=true;
            }

        }

    }

    private void addTimePointButton()
    {


        int x=this.default_x;
        int y=this.default_y-this.h-this.GAP_Y;
        timePointButton.setWidth(this.l1+this.l2);
        timePointButton.setHeight(this.h);
        timePointButton.setPosition(x,y);
        Component component=Component.translatable(GUI+"button.time_point",time_point);
        timePointButton.setMessage(component);

        addRenderableWidget(timePointButton);

    }



    private void addWidget()
    {
        this.current_y=this.default_y;
        int index=0;
        addButton(createTagButton("strength"),createOperateButton(index++));
        addButton(createTagButton("dexterity"),createOperateButton(index++));
        addButton(createTagButton("physique"),createOperateButton(index++));
        addButton(createTagButton("size"),createOperateButton(index++));
        addButton(createTagButton("intelligence"),createOperateButton(index++));
        addButton(createTagButton("meticulous"),createOperateButton(index++));
        addButton(createTagButton("knowledge"),createOperateButton(index++));
        addButton(createTagButton("will"),createOperateButton(index++));

        addTimePointButton();


        addComfirmButton();

    }

    private void addButton(Button attributeButton,List<Button> operateButtons)
    {
        attributeButton.setWidth(this.l1);
        attributeButton.setHeight(this.h);

        attributeButton.setPosition(current_x,current_y);

        addRenderableWidget(attributeButton);

        this.current_x=this.current_x+ GAP_X +this.l1;

        for(int i=0;i<7;i++)
        {
            Button button=operateButtons.get(i);
            button.setPosition(this.current_x,this.current_y);
            this.current_x+=button.getWidth();
            if(i==2||i==3)
            {
                this.current_x+=GAP_X;
            }
            addRenderableWidget(button);
        }

        this.current_y=this.current_y+ GAP_Y +this.h;
        this.current_x=this.default_x;

    }

    private void addComfirmButton()
    {
        Button comfirmButton= Button.builder(Component.translatable("gui.age_dust.button.comfirm"),b->save()).build();

        int l=100;
        int h=20;
        int n1 = this.width/5*3+GAP_X;
        int n2 = this.current_y;

        comfirmButton.setHeight(h);
        comfirmButton.setWidth(l);
        comfirmButton.setPosition(n1,n2);

        addRenderableWidget(comfirmButton);

    }

    public void save() {

        this.attributesInfo.levelArray=this.levelArrayToChange;
        ModChannels.sendToServer(new CharacterScreenClientPacket(characterInfo,this.attributesInfo));
        this.onClose();

        //Minecraft.getInstance().setScreen(new AttributeScreen(Component.literal(""),new AttributesInfo()));
    }
}
